package com.triones.api.controller.api;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.email.EmailValidationUtils;
import com.triones.api.entity.bo.LoginBO;
import com.triones.api.entity.bo.MemberBO;
import com.triones.api.entity.bo.MemberInfoBO;
import com.triones.api.entity.vo.MemberVO;
import com.triones.api.service.LogininforService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * api - 微信用户登录（公众号及小程序）
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api")
public class ApiAppLogininforController extends BaseController {

    private final LogininforService logininforService;

    /**
     * 获取邮箱验证码
     * @param email
     * @return
     */
    @SaIgnore
    @PostMapping("/emailCode/{type}")
    public R<Void> createEmailCode(@PathVariable String type, @RequestParam String email) {
        if (EmailValidationUtils.isValidEmail(email)) {
            throw new RuntimeException("邮箱格式不正确");
        }
        logininforService.createEmailCode(type,email);
        return R.ok("验证码已发送到您的邮箱，请查收。");
    }

    /**
     * 注册
     * @return
     */
    @SaIgnore
    @PostMapping("/register")
    public R<Void> register(@RequestBody MemberBO bo) {
        logininforService.register(bo);
        return R.ok("注册成功");
    }

    /**
     * 登录
     * @return
     */
    @SaIgnore
    @PostMapping("/login")
    public R<String> login(@RequestBody LoginBO bo) {
        return R.ok("登录成功！",logininforService.login(bo));
    }

    /**
     * 校验邮箱是否已被注册和是否合规
     * @return
     */
    @SaIgnore
    @GetMapping("/checkEmail")
    public R<Boolean> checkEmailUnique(@NotBlank( message = "邮箱不能为空") String email) {
        return R.ok(logininforService.checkEmailUnique(email));
    }

    /**
     * 获取用户基本信息
     * @return
     */
    @GetMapping("/memberInfo")
    public R<MemberVO> getMemberInfo() {
        return R.ok(logininforService.getMemberInfo());
    }

    /**
     * 编辑用户信息
     * @return
     */
    @PutMapping("/editMember")
    public R<Void> editMember(@RequestBody MemberInfoBO bo) {
        logininforService.editMember(bo);
        return R.ok("修改成功");
    }
}
